package cn.gcyl.module.biz.controller.admin.membershipsettings;

import cn.gcyl.framework.apilog.core.annotation.ApiAccessLog;
import cn.gcyl.framework.common.pojo.CommonResult;
import cn.gcyl.framework.common.pojo.PageParam;
import cn.gcyl.framework.common.pojo.PageResult;
import cn.gcyl.framework.common.util.object.BeanUtils;
import cn.gcyl.framework.excel.core.util.ExcelUtils;
import cn.gcyl.module.biz.controller.admin.membershipsettings.vo.*;
import cn.gcyl.module.biz.dal.dataobject.membershipsettings.MembershipSettingsDO;
import cn.gcyl.module.biz.service.membershipsettings.MembershipSettingsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.gcyl.framework.apilog.core.enums.OperateTypeEnum.*;
import static cn.gcyl.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 会员设置")
@RestController
@RequestMapping("/biz/membership-settings")
@Validated
public class MembershipSettingsController {

    @Resource
    private MembershipSettingsService membershipSettingsService;

    @PostMapping("/create")
    @Operation(summary = "创建会员设置")
//    @PreAuthorize("@ss.hasPermission('biz:membership-settings:create')")
    public CommonResult<Long> createMembershipSettings(@Valid @RequestBody MembershipSettingsSaveReqVO createReqVO) {
        return success(membershipSettingsService.createMembershipSettings(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新会员设置")
//    @PreAuthorize("@ss.hasPermission('biz:membership-settings:update')")
    public CommonResult<Boolean> updateMembershipSettings(@Valid @RequestBody MembershipSettingsSaveReqVO updateReqVO) {
        membershipSettingsService.updateMembershipSettings(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除会员设置")
    @Parameter(name = "id", description = "编号", required = true)
//    @PreAuthorize("@ss.hasPermission('biz:membership-settings:delete')")
    public CommonResult<Boolean> deleteMembershipSettings(@RequestParam("id") Long id) {
        membershipSettingsService.deleteMembershipSettings(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除会员设置")
//                @PreAuthorize("@ss.hasPermission('biz:membership-settings:delete')")
    public CommonResult<Boolean> deleteMembershipSettingsList(@RequestParam("ids") List<Long> ids) {
        membershipSettingsService.deleteMembershipSettingsListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得会员设置")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
//    @PreAuthorize("@ss.hasPermission('biz:membership-settings:query')")
    public CommonResult<MembershipSettingsRespVO> getMembershipSettings(@RequestParam("id") Long id) {
        MembershipSettingsDO membershipSettings = membershipSettingsService.getMembershipSettings(id);
        return success(BeanUtils.toBean(membershipSettings, MembershipSettingsRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得会员设置分页")
//    @PreAuthorize("@ss.hasPermission('biz:membership-settings:query')")
    public CommonResult<PageResult<MembershipSettingsRespVO>> getMembershipSettingsPage(@Valid MembershipSettingsPageReqVO pageReqVO) {
        PageResult<MembershipSettingsDO> pageResult = membershipSettingsService.getMembershipSettingsPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, MembershipSettingsRespVO.class));
    }

//    @GetMapping("/export-excel")
//    @Operation(summary = "导出会员设置 Excel")
////    @PreAuthorize("@ss.hasPermission('biz:membership-settings:export')")
//    @ApiAccessLog(operateType = EXPORT)
//    public void exportMembershipSettingsExcel(@Valid MembershipSettingsPageReqVO pageReqVO,
//              HttpServletResponse response) throws IOException {
//        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
//        List<MembershipSettingsDO> list = membershipSettingsService.getMembershipSettingsPage(pageReqVO).getList();
//        // 导出 Excel
//        ExcelUtils.write(response, "会员设置.xls", "数据", MembershipSettingsRespVO.class,
//                        BeanUtils.toBean(list, MembershipSettingsRespVO.class));
//    }

}